package com.bloveoacom.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import com.blankj.utilcode.util.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.text.TextUtils.isEmpty;
import static com.bloveoacom.http.HttpApi.HTMLBASEURL;
import static com.bloveoacom.http.HttpApi.PICBASEURL;
import static com.bloveoacom.http.HttpApi.PIC_HTML_BASEURL;
import static java.util.regex.Pattern.compile;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/20
 * desc  :
 */
public class StringUtils {
    public static String getFullUrl(String url, int type) {
        if (isUrl(url)) {
            return url;
        } else {
            switch (type) {
                case 0:
                    url = PICBASEURL + url;
                    break;
                case 1:
                    url = PIC_HTML_BASEURL + url;
                    break;
                case 2:
                    url = HTMLBASEURL + url;
                    break;
                default:
                    url = PICBASEURL + url;
                    break;
            }
            return url;
        }
    }
    public static boolean isUrl(String httpurl) {
        if (!isEmpty(httpurl)) {
            return httpurl.contains("http://")||httpurl.contains("https://");
        } else
            return false;
    }

    public static boolean isEmpty(CharSequence str) {
        if (str == null || str.equals("") || str.equals("null")) {
            return true;
        }
        return false;
    }
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
    public static List<String> cutStringByImgTag(String targetStr) {
        List<String> splitTextList = new ArrayList<>();
        Pattern pattern = compile("<img.*?src=\"(.*?)\".*?>");
        Matcher matcher = pattern.matcher(targetStr);
        int lastIndex = 0;
        while (matcher.find()) {
            if (matcher.start() > lastIndex) {
                splitTextList.add(targetStr.substring(lastIndex, matcher.start()));
            }
            splitTextList.add(targetStr.substring(matcher.start(), matcher.end()));
            lastIndex = matcher.end();
        }
        if (lastIndex != targetStr.length()) {
            splitTextList.add(targetStr.substring(lastIndex, targetStr.length()));
        }
        return splitTextList;
    }
    public static void copyText(final CharSequence text) {
        ClipboardManager clipboard = (ClipboardManager) Utils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText("text", text));
    }

    public static String getData(String data){
         if(data==null){
             return "";
         }
         return data;
    }


    public static String replaceNTB(String data){
        if(data.contains("\n")){
          return  data.replaceAll("\n","<br />");
        }else{
            return data;
        }
    }



}
